var inited=false;
$(document).ready(function(){
    console.log("loading attack");
    initOptions();
})

function initOptions(){
    //配置攻击源地图
    var option=getAttackSourceTreeOption();
    var myChart = echarts.init(document.getElementById('attackSourceChart'));
    myChart.setOption(option);
    //配置攻击比例饼图
    option=getAttackTypePieOption();
    myChart = echarts.init(document.getElementById('attackTypeChart'));
    myChart.setOption(option);
    //配置攻击事件溯源地图
    $.getJSON('./lib/china_geo.json', function (chinaJson) {
        echarts.registerMap('china', chinaJson);
        var chart = echarts.init(document.getElementById('attackEventDetailChart'));
        chart.setOption(getAttackEventSourceMapOption());
    });
/*     option=getAttackEventSourceMapOption();
    myChart = echarts.init(document.getElementById('attackEventDetailChart'));
    myChart.setOption(option); */
    inited=true;
}

function loadWebStreamData(){

}

function loadFirewallStreamData(){

}

function loadAbnormalData(){

}

function loadAttackData(){
    if(!inited){
        setTimeout(loadAttackData,100);
        return;
    }
    //获取数据
    var data=isrealMode ? REAL_MODE_DATA : QUERY_MODE_DATA;
    //处理数据
    let eventsTableData=[];
    let pieData={};
    let treeData={};
    data.attackData.data.forEach((event,index)=>{
        //更新饼图数据
        pieData[event.feature['type']] ? pieData[event.feature['type']]+=1 : pieData[event.feature['type']]=1;
        //更新表格数据
        eventsTableData.push([index+1,event.startTime,event.feature['type'],event.feature['srcIp'],event.feature['srcCity'],`<a href='#' onclick="loadEventDetail('${event.feature['type']}','${event.startTime}')">查看详情</a>`]);
        //更新树图数据
        let city=event.feature['srcCity'];
        let ip=event.feature['srcIp']; 
        if(city!="不详"){
            if(treeData[city]){
                if(treeData[city][ip]){
                    treeData[city][ip]+=1;
                }
                else
                treeData[city][ip]=1;
            }
            else{
                treeData[city]={};
                treeData[city][ip]=1;
            } 
        }
    });
    //更新攻击源树图
    let totalNum=0;
    let result=[];
    let childResult=[];
    for (const city in treeData) {
        let num=0;
        childResult=[];
        for (const ip in treeData[city]) {
            let value=treeData[city][ip];
            totalNum+=value;
            num+=value;
            childResult.push({name:`${ip}\n(共发起${value}次)`,value:value});
        };
        result.push({
            name:`${city}\n共发起${num}次`,
            value:num,
            children:childResult
        });
    };
    //应用数据
    var chart = echarts.getInstanceByDom(document.getElementById('attackSourceChart'));
    var option={
        series:[{
            data:[{
                name:"受监测目标",
                value:totalNum,
                children:result
            }]
        }]
    };
    chart.setOption(option);
    //更新攻击事件比例图
    result=[];
    for (const attack in pieData) {
        result.push({ name:attack, value: pieData[attack]});
    };

    //应用数据
    var chart = echarts.getInstanceByDom(document.getElementById('attackTypeChart'));
    var option={
        series:[{
            data:result
        }]
    };
    chart.setOption(option);
    //更新攻击事件表格
    $("#attackEventsTable").DataTable({
        data:eventsTableData,
        columns: [
            { title: "序号" },
            { title: "发生时间" },
            { title: "攻击类型" },
            { title: "源IP" },
            { title: "源城市" },
            { title: "操作" },    
        ],
        paging:true,
        pageLength:10,
        scrollY:130,
        lengthChange:true,
        searching:true,
        info:false,
        ordering:true,
        destroy:true,
        select:true
    });


/*         for (const event in data.attackEvents) {
        result.push([index,e,ipAbnormalData[dstIp]+"次"]);
    };
    this.ipAbnormalTableData=result;
*/
}

/**
 * 查看事件详情
 */
function loadEventDetail(type,time){
    $.getJSON('./assets/mockdata/attackEventDetail.json', function (data) {
        var detail;
        //console.log(data,data[type],type);
        data[type].forEach(element => {
            if(element.startTime==time)
                detail=element;
        });

        if(detail!=null){
            $("#attackName").text(detail.type);
            $("#attackStartTime").text(detail.startTime);
            $("#attackEndTime").text(detail.endTime);
            //$("#attackFeature").text(detail.feature);
            $("#attackDesc").text(detail.type);
            $("#attackMeasure").text(detail.measure);

            var city=detail.srcCity,
            coord=parseCityToCoord(city),
                option={};
            if(city!="不详" && city!="域外")
                option={
                    series:{
                        data:[[coord[0],coord[1],1,city]]
                    }
                };
            else{
                option={
                    series:{
                        data:[]
                    }
                };
            }
            var chart = echarts.getInstanceByDom(document.getElementById('attackEventDetailChart'));
            chart.setOption(option);
        }
    });
}